<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Gemini","version":"8.7.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>
<meta name="description" content="模板写法一、完整版，写在HTML里123456789101112&lt;div id&#x3D;&amp;#x27;xxx&amp;#x27;&gt;	&amp;#123;&amp;#123; n &amp;#125;&amp;#125;    &lt;button @click&#x3D;&quot;add&quot;&gt;+&#x3D;1&lt;&#x2F;button&gt;&lt;&#x2F;div&gt;new Vue(&amp;#123;    el: &amp;#x27;#xxx&amp;#x27;,">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue模板">
<meta property="og:url" content="http://example.com/2021/10/20/01/index.html">
<meta property="og:site_name" content="Xiaolong&#39;s Blog">
<meta property="og:description" content="模板写法一、完整版，写在HTML里123456789101112&lt;div id&#x3D;&amp;#x27;xxx&amp;#x27;&gt;	&amp;#123;&amp;#123; n &amp;#125;&amp;#125;    &lt;button @click&#x3D;&quot;add&quot;&gt;+&#x3D;1&lt;&#x2F;button&gt;&lt;&#x2F;div&gt;new Vue(&amp;#123;    el: &amp;#x27;#xxx&amp;#x27;,">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-10-20T11:45:01.000Z">
<meta property="article:modified_time" content="2021-10-21T04:31:54.306Z">
<meta property="article:author" content="Xiaolong">
<meta property="article:tag" content="vue">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/10/20/01/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"en","comments":true,"permalink":"http://example.com/2021/10/20/01/","path":"2021/10/20/01/","title":"Vue模板"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Vue模板 | Xiaolong's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Xiaolong's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A8%A1%E6%9D%BF%E5%86%99%E6%B3%95"><span class="nav-number">1.</span> <span class="nav-text">模板写法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%AE%8C%E6%95%B4%E7%89%88%EF%BC%8C%E5%86%99%E5%9C%A8HTML%E9%87%8C"><span class="nav-number">1.1.</span> <span class="nav-text">一、完整版，写在HTML里</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%AE%8C%E6%95%B4%E7%89%88%EF%BC%8C%E5%86%99%E5%9C%A8%E9%80%89%E9%A1%B9%E9%87%8C"><span class="nav-number">1.2.</span> <span class="nav-text">二、完整版，写在选项里</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E9%9D%9E%E5%AE%8C%E6%95%B4%E7%89%88%EF%BC%8C%E9%85%8D%E5%90%88-xxx-vue-%E5%8D%95%E6%96%87%E4%BB%B6%E7%BB%84%E4%BB%B6"><span class="nav-number">1.3.</span> <span class="nav-text">三、非完整版，配合 xxx.vue 单文件组件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B1%95%E7%A4%BA%E5%86%85%E5%AE%B9"><span class="nav-number">2.</span> <span class="nav-text">展示内容</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%91%E5%AE%9A%E5%B1%9E%E6%80%A7-v-bind"><span class="nav-number">3.</span> <span class="nav-text">绑定属性 v-bind</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%91%E5%AE%9A%E4%BA%8B%E4%BB%B6-v-on"><span class="nav-number">4.</span> <span class="nav-text">绑定事件 v-on</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD-v-if-v-else-if-v-else"><span class="nav-number">5.</span> <span class="nav-text">条件判断 v-if v-else-if v-else</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BE%AA%E7%8E%AF-v-for"><span class="nav-number">6.</span> <span class="nav-text">循环 v-for</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%98%BE%E7%A4%BA-%E9%9A%90%E8%97%8F-v-show"><span class="nav-number">7.</span> <span class="nav-text">显示&#x2F;隐藏 v-show</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#v-clock"><span class="nav-number">8.</span> <span class="nav-text">v-clock</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#v-once"><span class="nav-number">9.</span> <span class="nav-text">v-once</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8C%87%E4%BB%A4-directive"><span class="nav-number">10.</span> <span class="nav-text">指令 directive</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E6%8C%87%E4%BB%A4%EF%BC%9F"><span class="nav-number">10.1.</span> <span class="nav-text">什么是指令？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E6%B3%95"><span class="nav-number">10.2.</span> <span class="nav-text">语法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="nav-number">11.</span> <span class="nav-text">修饰符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#sync-%E9%87%8D%E7%82%B9"><span class="nav-number">12.</span> <span class="nav-text">.sync (重点)</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-overview">
            <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Xiaolong"
      src="/images/lion.jpg">
  <p class="site-author-name" itemprop="name">Xiaolong</p>
  <div class="site-description" itemprop="description">Never stop learning</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">45</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/drwna" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;drwna" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



          </div>
        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/Drwna" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/10/20/01/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Vue模板
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-10-20 19:45:01" itemprop="dateCreated datePublished" datetime="2021-10-20T19:45:01+08:00">2021-10-20</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="模板写法"><a href="#模板写法" class="headerlink" title="模板写法"></a>模板写法</h2><h3 id="一、完整版，写在HTML里"><a href="#一、完整版，写在HTML里" class="headerlink" title="一、完整版，写在HTML里"></a>一、完整版，写在HTML里</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=<span class="string">&#x27;xxx&#x27;</span>&gt;</span><br><span class="line">	&#123;&#123; n &#125;&#125;</span><br><span class="line">    &lt;button @click=<span class="string">&quot;add&quot;</span>&gt;+=<span class="number">1</span>&lt;/button&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">el</span>: <span class="string">&#x27;#xxx&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>: &#123; <span class="attr">n</span>: <span class="number">5</span> &#125;, <span class="comment">// data 可以写成函数</span></span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">add</span>(<span class="params"></span>)&#123;<span class="variable language_">this</span>.<span class="property">n</span> += <span class="number">1</span>&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<span id="more"></span>

<h3 id="二、完整版，写在选项里"><a href="#二、完整版，写在选项里" class="headerlink" title="二、完整版，写在选项里"></a>二、完整版，写在选项里</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=<span class="string">&#x27;app&#x27;</span>&gt;&lt;/div&gt;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string">		&lt;div&gt;</span></span><br><span class="line"><span class="string">			&#123;&#123; n &#125;&#125;</span></span><br><span class="line"><span class="string">			&lt;button @click=&quot;add&quot;&gt;+1&lt;/button&gt;</span></span><br><span class="line"><span class="string">		&lt;/div&gt;`</span>,</span><br><span class="line">    <span class="attr">data</span>:&#123; <span class="attr">n</span>: <span class="number">5</span> &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123; <span class="title function_">add</span>(<span class="params"></span>)&#123; <span class="variable language_">this</span>.<span class="property">n</span> += <span class="number">1</span>&#125; &#125;</span><br><span class="line">&#125;).$mount(<span class="string">&#x27;#app&#x27;</span>) <span class="comment">// 和 el 相同</span></span><br></pre></td></tr></table></figure>



<h3 id="三、非完整版，配合-xxx-vue-单文件组件"><a href="#三、非完整版，配合-xxx-vue-单文件组件" class="headerlink" title="三、非完整版，配合 xxx.vue 单文件组件"></a>三、非完整版，配合 xxx.vue 单文件组件</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">// xxx.vue</span><br><span class="line">&lt;template&gt;</span><br><span class="line">// template里不是 HTML, 而是 XML</span><br><span class="line">	&lt;div&gt;</span><br><span class="line">        &#123;&#123;n&#125;&#125;</span><br><span class="line">        &lt;button @click=&#x27;add&#x27;&gt; +1 &lt;/button&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">    export default &#123;</span><br><span class="line">        data()&#123; // data 必须为函数</span><br><span class="line">            return &#123; n: 0 &#125; </span><br><span class="line">        &#125;，</span><br><span class="line">        method: &#123; add() &#123; this.n +=1 &#125;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line">&lt;style&gt;...css&lt;/style&gt;</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">Xxx</span> <span class="keyword">from</span> <span class="string">&#x27;./xxx.vue&#x27;</span></span><br><span class="line"><span class="comment">// Xxx 是一个options 对象</span></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">render</span>: <span class="function"><span class="params">h</span> =&gt;</span> <span class="title function_">h</span>(<span class="title class_">Xxx</span>)</span><br><span class="line">&#125;).$mount(<span class="string">&#x27;#app&#x27;</span>)</span><br></pre></td></tr></table></figure>



<h2 id="展示内容"><a href="#展示内容" class="headerlink" title="展示内容"></a>展示内容</h2><ul>
<li><p>表达式</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#123; object.<span class="property">a</span> &#125;&#125; <span class="comment">// 表达式</span></span><br><span class="line">&#123;&#123; n + <span class="number">1</span> &#125;&#125; <span class="comment">// 可以写任何运算</span></span><br><span class="line">&#123;&#123; <span class="title function_">fn</span>(n) &#125;&#125; <span class="comment">// 可以调用函数</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">如果值 为 undefined 或 null 就不显示</span></span><br><span class="line"><span class="comment">另一种写法为 &lt;div v-text=&quot;表达式&quot;&gt;&lt;/div&gt;</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure></li>
<li><p>HTML 内容</p>
<p>假设 <code>data.x</code> 值为 <code>&lt;strong&gt;hi&lt;/strong&gt;</code></p>
<p><code>&lt;div v-html=&#39;x&#39;&gt;&lt;/div&gt;</code> 即可显示粗体的 hi</p>
</li>
<li><p>展示 </p>
<p><code>&lt;div v-pre&gt;&#123;&#123; n &#125;&#125;&lt;/div&gt;</code></p>
<p><code>v-pre</code> 不会对模板进行编译</p>
</li>
</ul>
<h2 id="绑定属性-v-bind"><a href="#绑定属性-v-bind" class="headerlink" title="绑定属性 v-bind"></a>绑定属性 v-bind</h2><ul>
<li><p>绑定 src</p>
<p><code>&lt;img v-bind:src=&quot;x&quot; /&gt;</code></p>
</li>
<li><p>v-bind: 简写为：</p>
<p><code>&lt;img :src=&quot;x&quot; /&gt;</code></p>
</li>
<li><p>绑定对象</p>
<p><code>&lt;div :style=&quot;&#123;border: &#39;1px solid red, height: 100&#39;&#125;&quot;&gt;&lt;/div&gt;</code></p>
</li>
</ul>
<h2 id="绑定事件-v-on"><a href="#绑定事件-v-on" class="headerlink" title="绑定事件 v-on"></a>绑定事件 v-on</h2><ul>
<li><p>v-on : 事件名</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;button v-on:click=&quot;add&quot;&gt;+1&lt;/button&gt;</span><br><span class="line">点击之后，Vue 会运行 add()</span><br><span class="line"></span><br><span class="line">&lt;button v-on:click=&quot;xxx(1)&quot;&gt;xxx&lt;/button&gt;</span><br><span class="line">点击之后，Vue 会运行 xxx(1)</span><br><span class="line"></span><br><span class="line">&lt;button v-on:click=&quot;n+=1&quot;&gt;xxx&lt;/button&gt;</span><br><span class="line">点击之后，Vue 会运行 n += 1</span><br><span class="line"></span><br><span class="line">即发现 函数就加括号调用之，否咋就直接运行代码</span><br></pre></td></tr></table></figure></li>
<li><p>缩写</p>
<p><code>&lt;button @click&quot;add&quot;&gt;+ 1&lt;/button&gt;</code>, 正常人都用缩写🤗</p>
</li>
</ul>
<h2 id="条件判断-v-if-v-else-if-v-else"><a href="#条件判断-v-if-v-else-if-v-else" class="headerlink" title="条件判断 v-if v-else-if v-else"></a>条件判断 v-if v-else-if v-else</h2><p><code>if ... else</code></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">// 不处于 DOM 树中</span><br><span class="line">&lt;div v-if=&#x27;x&gt;0&#x27;&gt;</span><br><span class="line">    x 大于 0</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">&lt;div v-else-if=&#x27;x===0&#x27;&gt;</span><br><span class="line">    x 为 0</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">&lt;div v-else&gt;</span><br><span class="line">    x 小于 0</span><br><span class="line">&lt;/div&gt;</span><br></pre></td></tr></table></figure>



<h2 id="循环-v-for"><a href="#循环-v-for" class="headerlink" title="循环 v-for"></a>循环 v-for</h2><p><code>for(value, key) in 对象 或 数组</code></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;ul&gt;</span><br><span class="line">    &lt;li v-for=&quot;(u, index) in users&quot; :key=&quot;index&quot;&gt;</span><br><span class="line">    	索引：&#123;&#123;index&#125;&#125; 值：&#123;&#123;u.name&#125;&#125;</span><br><span class="line">    &lt;/li&gt;</span><br><span class="line">&lt;/ul&gt;</span><br><span class="line">// :key=&quot;xxx&quot; 必需</span><br><span class="line">&lt;ul&gt;</span><br><span class="line">    &lt;li v-for=&quot;(value, name) in obj&quot; :key=&quot;name&quot;&gt;</span><br><span class="line">    	属性名：&#123;&#123;name&#125;&#125; 属性值：&#123;&#123;name&#125;&#125;</span><br><span class="line">    &lt;/li&gt;</span><br><span class="line">&lt;/ul&gt;</span><br></pre></td></tr></table></figure>



<h2 id="显示-隐藏-v-show"><a href="#显示-隐藏-v-show" class="headerlink" title="显示/隐藏 v-show"></a>显示/隐藏 v-show</h2><p><code>v-show</code></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;div v-show=&quot;n % 2 === 0&quot;&gt;</span><br><span class="line">    n 是偶数</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">近似等于</span><br><span class="line">&lt;div :style=&quot;display: n % 2 === 0 ? &#x27;block&#x27; : &#x27;none&#x27;&quot;&gt;</span><br><span class="line">    n 是偶数</span><br><span class="line">&lt;/div&gt;</span><br></pre></td></tr></table></figure>



<h2 id="v-clock"><a href="#v-clock" class="headerlink" title="v-clock"></a><a target="_blank" rel="noopener" href="https://www.jianshu.com/p/f56cde007210">v-clock</a></h2><p>HTML 绑定 Vue实例，在页面加载时会闪烁，在 css 中添加 <code>[v-clock]&#123; display: none; &#125;</code></p>
<p><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#v-cloak">官方API</a></p>
<h2 id="v-once"><a href="#v-once" class="headerlink" title="v-once"></a><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#v-once">v-once</a></h2><p>v-once 这个指令不需要任何表达式，它的作用就是定义它的元素或组件只会渲染一次，包括元素或者组件的所有字节点。首次渲染后，不再随着数据的改变而重新渲染。也就是说使用 v-once，那么该块都将被视为静态内容。</p>
<p>如果显示的信息后续不需要再修改，使用 v-once，这样可以提高性能。</p>
<h2 id="指令-directive"><a href="#指令-directive" class="headerlink" title="指令 directive"></a>指令 directive</h2><h3 id="什么是指令？"><a href="#什么是指令？" class="headerlink" title="什么是指令？"></a>什么是指令？</h3><p><code>&lt;div v-text=&quot;x&quot;&gt;&lt;/div&gt;</code></p>
<p><code>&lt;div v-html=&quot;x&quot;&gt;&lt;/div&gt;</code></p>
<p>以 <strong>v-</strong> 开头的就是指令。</p>
<h3 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h3><p>v -指令名：参数 = 值，例：<code>v-on:click = add</code></p>
<p>如果<strong>值</strong>里没有特殊字符，则可以不加引号</p>
<p>有些指令没有参数和值，例：<code>v-pre</code></p>
<p>有些指令没有值，例：<code>v-on:click.prevent</code>阻止默认事件，<code>@click.prevent=&#39;x&#39;</code>阻止默认事件并执行 x 函数</p>
<h2 id="修饰符"><a href="#修饰符" class="headerlink" title="修饰符"></a><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/guide/events.html#%E4%BA%8B%E4%BB%B6%E4%BF%AE%E9%A5%B0%E7%AC%A6">修饰符</a></h2><p>有些指令支持修饰符</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@click.stop = &quot;add&quot; 表示阻止事件传播/冒泡</span><br><span class="line">@click.prevent = &quot;add&quot; 表示阻止模拟动作</span><br><span class="line">@click.stop.prevent = &quot;add&quot; 表示以上两种意思</span><br><span class="line">@keypress.enter=&quot;xxx&quot; 表示键盘按下Enter 执行 xxx</span><br></pre></td></tr></table></figure>

<p>一共多少修饰符？</p>
<p><code>v-on</code> 支持的有</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">.&#123;keycode | keyAlias&#125;</span><br><span class="line"></span><br><span class="line">.<span class="property">stop</span>	.<span class="property">prevent</span>	.<span class="property">capture</span>	.<span class="property">self</span>	.<span class="property">once</span>	.<span class="property">passive</span>	.<span class="property">native</span></span><br><span class="line"></span><br><span class="line">快捷键相关	.<span class="property">ctrl</span>	.<span class="property">alt</span>	.<span class="property">shift</span>	.<span class="property">meta</span>	.<span class="property">exact</span></span><br><span class="line"></span><br><span class="line">鼠标相关	.<span class="property">left</span>	.<span class="property">right</span>	.<span class="property">middle</span></span><br></pre></td></tr></table></figure>

<p><code>v-bind</code> 支持的有</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.<span class="property">prop</span>	.<span class="property">camel</span>	.<span class="property">sync</span></span><br></pre></td></tr></table></figure>

<p><code>v-model</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.<span class="property">lazy</span>	.<span class="property">number</span>	.<span class="property">trim</span></span><br></pre></td></tr></table></figure>



<p><strong>重点记忆：</strong></p>
<p><code>.stop</code> <code>.prevent</code> <code>.sync</code></p>
<h2 id="sync-重点"><a href="#sync-重点" class="headerlink" title=".sync (重点)"></a><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/guide/components-custom-events.html#sync-%E4%BF%AE%E9%A5%B0%E7%AC%A6">.sync (重点)</a></h2><p>场景描述：爸爸给儿子钱，儿子要 花钱怎么办，<a target="_blank" rel="noopener" href="https://codesandbox.io/s/small-leftpad-zzvpp">示例</a></p>
<p>答：儿子打电话（触发事件）向爸爸要钱。</p>
<p><strong>vue规则：</strong></p>
<ul>
<li>组件不能修改 props 外部数据</li>
<li><code>this.$emit</code> 可以触发事件，并传参</li>
<li><code>$event</code> 可以获取 <code>$emit</code> 的参数</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://codesandbox.io/s/small-leftpad-zzvpp">上述示例使用 .sync 后。</a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">:money.<span class="property">sync</span>=<span class="string">&quot;tota&quot;</span></span><br><span class="line">上下等价</span><br><span class="line">:money=<span class="string">&quot;total&quot;</span> v-<span class="attr">on</span>:<span class="attr">update</span>:money=<span class="string">&quot;total=$event&quot;</span></span><br></pre></td></tr></table></figure>

<p><strong>这样实现了父子组件的双向绑定</strong></p>
<p><a target="_blank" rel="noopener" href="https://v3.vuejs.org/guide/migration/v-model.html#v-model-modifiers">Vue3 推荐使用 v-model 替代 sync</a></p>
<hr>
<p>子组件使用 $emit 向父组件发送事件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">this</span>.$emit(<span class="string">&#x27;update:title&#x27;</span>, newTitle)</span><br></pre></td></tr></table></figure>

<p>父组件监听这个事件并更新数据</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;text-document</span><br><span class="line">  v-bind:title=&quot;doc.title&quot;</span><br><span class="line">  v-on:update:title=&quot;doc.title = $event&quot;</span><br><span class="line">&gt;&lt;/text-document&gt;</span><br></pre></td></tr></table></figure>

<p>为了方便这种写法，vue 提供了 <strong>.sync</strong> 修饰符，也就是一种简写方式语法糖：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;text-document v-bind.sync=&quot;doc&quot;&gt;&lt;/text-document&gt;</span><br></pre></td></tr></table></figure>



<blockquote>
<p><a target="_blank" rel="noopener" href="http://www.jsphp.net/vue/show-25-291-1.html">vue.sync的用法</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/vue/" rel="tag"><i class="fa fa-tag"></i> vue</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/10/18/31/" rel="prev" title="vue构造选项">
                  <i class="fa fa-chevron-left"></i> vue构造选项
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/10/22/47/" rel="next" title="router-demo">
                  router-demo <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2021 – 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Xiaolong</span>
</div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/third-party/search/local-search.js"></script>




  





</body>
</html>
